﻿Namespace DotNetNuke.Modules.ActiveForums
    Public Class URL
        Public Shared Function ForumLink(ByVal TabId As Integer, ByVal fi As Forum) As String
            Dim _mainSettings As SettingsInfo = DataCache.MainSettings(fi.ModuleId)
            Dim sURL As String = String.Empty
            If String.IsNullOrEmpty(fi.PrefixURL) Or Not _mainSettings.URLRewriteEnabled Then
                sURL = Utilities.NavigateUrl(TabId, String.Empty, ParamKeys.ForumId & "=" & fi.ForumID.ToString)
            Else
                If Not String.IsNullOrEmpty(_mainSettings.PrefixURLBase) Then
                    sURL = "/" & _mainSettings.PrefixURLBase
                End If
                If Not String.IsNullOrEmpty(fi.ForumGroup.PrefixURL) Then
                    sURL &= "/" & fi.ForumGroup.PrefixURL
                End If
                sURL &= "/" & fi.PrefixURL & "/"
            End If
            Dim sHost As String = Utilities.GetHost()
            If Not sURL.StartsWith(sHost) Then
                If sHost.EndsWith("/") Then
                    sHost = sHost.Substring(0, sHost.Length - 1)
                End If
                sURL = sHost & sURL
            End If
            Return sURL
        End Function
        Public Shared Function TopicLink(ByVal TabId As Integer, ByVal ModuleId As Integer, ByVal ti As TopicInfo) As String
            Dim db As New Data.Common
            Dim sURL As String = String.Empty
            Dim _mainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
            If String.IsNullOrEmpty(ti.URL) Or Not _mainSettings.URLRewriteEnabled Then
                sURL = Utilities.NavigateUrl(TabId, String.Empty, ParamKeys.TopicId & "=" & ti.TopicId)
            Else
                sURL = "/" & db.GetUrl(ModuleId, -1, -1, ti.TopicId, -1, -1)
            End If
            Dim sHost As String = Utilities.GetHost()
            If Not sURL.StartsWith(sHost) Then
                If sHost.EndsWith("/") Then
                    sHost = sHost.Substring(0, sHost.Length - 1)
                End If
                sURL = sHost & sURL
            End If
            Return sURL
        End Function
        Public Shared Function ReplyLink(ByVal TabId As Integer, ByVal ti As TopicInfo, ByVal UserId As Integer, ByVal ReplyId As Integer) As String
            Dim db As New Data.Common
            Dim sURL As String = Utilities.NavigateUrl(TabId, String.Empty, New String() {ParamKeys.TopicId & "=" & ti.TopicId, ParamKeys.ContentJumpId & "=" & ReplyId})
            If String.IsNullOrEmpty(ti.URL) Or Not Utilities.IsRewriteLoaded Then
                Return sURL
            Else
                sURL = db.GetUrl(-1, -1, -1, ti.TopicId, UserId, ReplyId)
                If Not String.IsNullOrEmpty(sURL) Then
                    Dim sHost As String = Utilities.GetHost()
                    If sURL.StartsWith("/") Then
                        sURL = sURL.Substring(1)
                    End If
                    If Not sHost.EndsWith("/") Then
                        sHost &= "/"
                    End If
                    sURL = sHost & sURL
                    If Not sURL.EndsWith("/") Then
                        sURL &= "/"
                    End If
                    If ReplyId > 0 Then
                        sURL &= "#" & ReplyId.ToString
                    End If
                End If
            End If
            Return sURL


        End Function
        Public Shared Function ForForum(ByVal PageId As Integer, ByVal ForumId As Integer, ByVal GroupName As String, ByVal ForumName As String) As String
            Dim sURL As String = Utilities.NavigateUrl(PageId, "", New String() {ParamKeys.ForumId & "=" & ForumId})
            sURL = sURL.ToLowerInvariant
            Dim sNewPage As String = String.Empty
            If Not String.IsNullOrEmpty(GroupName) Then
                sNewPage = Utilities.CleanStringForUrl(GroupName) & "/"
            End If
            If Not String.IsNullOrEmpty(ForumName) Then
                sNewPage &= Utilities.CleanStringForUrl(ForumName)
            End If
            If Not String.IsNullOrEmpty(sNewPage) Then
                sURL = sURL.Replace("default.aspx", sNewPage & ".aspx")
            End If
            Return sURL.ToLowerInvariant
        End Function

        Public Shared Function ForTopic(ByVal PageId As Integer, ByVal PortalId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer) As String
            Return ForTopic(PageId, PortalId, ForumId, TopicId, String.Empty, String.Empty, String.Empty, 1)
        End Function
        Public Shared Function ForTopic(ByVal PageId As Integer, ByVal PortalId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal GroupName As String, ByVal ForumName As String, ByVal Subject As String, ByVal PageNumber As Integer) As String
            Dim sURL As String = String.Empty
            If PageNumber > 1 Then
                '        sURL = Utilities.NavigateUrl(PageId, "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId, ParamKeys.PageId & "=" & PageNumber})
                sURL = Utilities.NavigateUrl(PageId, "", Subject, PortalId, New String() {ParamKeys.TopicId & "=" & TopicId, ParamKeys.PageId & "=" & PageNumber})
            Else
                'sURL = Utilities.NavigateUrl(PageId, "", New String() {ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId})
                sURL = Utilities.NavigateUrl(PageId, "", Subject, PortalId, New String() {ParamKeys.TopicId & "=" & TopicId})
            End If
            sURL = sURL.ToLowerInvariant
            If Not sURL.EndsWith(".aspx") Then
                sURL &= ".aspx"
            End If
            Return sURL
            Dim sNewPage As String = String.Empty
            If Not String.IsNullOrEmpty(GroupName) Then
                sNewPage = Utilities.CleanStringForUrl(GroupName) & "/"
            End If
            If Not String.IsNullOrEmpty(ForumName) Then
                sNewPage &= Utilities.CleanStringForUrl(ForumName) & "/"
            End If
            If Not String.IsNullOrEmpty(Subject) Then
                sNewPage &= Utilities.CleanStringForUrl(Subject)
            End If
            If Not String.IsNullOrEmpty(sNewPage) Then
                sURL = sURL.Replace("default.aspx", sNewPage & ".aspx")
            End If
            Return sURL.ToLowerInvariant
        End Function

    End Class
End Namespace